import Mock from 'mockjs';
import type { MockRequestOptions } from '@/types/mock';

// 定义返回的用户数据类型
interface User {
  id: number;
  name: string;
  email: string;
  avatar: string;
  createdAt: string;
}

// 模拟用户列表数据
export const mockUserList = Mock.mock({
  'list|10-20': [
    {
      'id|+1': 1,
      'name': '@cname',
      'email': '@email',
      'avatar': '@image(200x200)',
      'createdAt': '@datetime',
    } as unknown as User,
  ],
});

// 模拟单个用户数据
export const mockUserDetail = (id: number) => {
  return Mock.mock({
    id,
    'name': '@cname',
    'email': '@email',
    'avatar': '@image(200x200)',
    'createdAt': '@datetime',
  } as User);
};

// 定义 API 接口
export default [
  // 获取用户列表
  {
    url: '/api/users',
    method: 'get',
    response: () => {
      return {
        code: 200,
        message: 'success',
        data: mockUserList.list,
      };
    },
  },
  // 获取单个用户
  {
    url: '/api/users/(@id)',
    method: 'get',
    response: (options: MockRequestOptions) => {
      const id = parseInt(options.url.split('/').pop() || '1', 10);
      return {
        code: 200,
        message: 'success',
        data: mockUserDetail(id),
      };
    },
  },
];